package dao

import (
	"fmt"
	__ "hospital-srv/basic/proto"

	"hospital-srv/handler/models"
)

// 设备添加
func EquipmentAdd(in *__.EquipmentAddReq) (*models.Equipment, error) {
	var equipment models.Equipment
	var brand models.Brand
	err := brand.GetBrandId(int(in.BrandId))
	if err != nil {
		return nil, fmt.Errorf("品牌查找失败")
	}
	if brand.Id == 0 {
		return nil, fmt.Errorf("品牌不存在")
	}
	equipment.BrandId = in.BrandId
	equipment.EquipmentName = in.EquipmentName
	equipment.EquipmentModel = in.EquipmentModel
	equipment.Manufacturer = in.Manufacturer
	err = equipment.EquipmentAdd()
	if err != nil {
		return nil, fmt.Errorf("设备添加失败" + err.Error())
	}
	return &equipment, nil
}

// 设备删除
func EquipmentDel(in *__.EquipmentDelReq) (*models.Equipment, error) {
	var equipment models.Equipment
	equipment.Id = uint64(in.Id)
	err := equipment.EquipmentDel()
	if err != nil {
		return nil, fmt.Errorf("设备删除失败" + err.Error())
	}
	return &equipment, nil
}

// 设备修改
func EquipmentUpdate(in *__.EquipmentUpdateReq) (*models.Equipment, error) {
	var equipment models.Equipment
	equipment.Id = uint64(in.Id)
	err := equipment.EquipmentId()
	if err != nil {
		return nil, fmt.Errorf("设备查找失败")
	}
	if equipment.Id == 0 {
		return nil, fmt.Errorf("设备不存在")
	}
	var brand models.Brand
	err = brand.GetBrandId(int(in.BrandId))
	if err != nil {
		return nil, fmt.Errorf("品牌查找失败")
	}
	if brand.Id == 0 {
		return nil, fmt.Errorf("品牌不存在")
	}
	equipment.BrandId = in.BrandId
	equipment.EquipmentName = in.EquipmentName
	equipment.EquipmentModel = in.EquipmentModel
	equipment.Manufacturer = in.Manufacturer
	err = equipment.EquipmentUpdate()
	if err != nil {
		return nil, fmt.Errorf("设备修改失败" + err.Error())
	}
	return &equipment, nil
}

// 设备展示
func EquipmentFind(in *__.EquipmentFindReq) ([]*__.EquipmentFind, error) {
	var equipment models.Equipment
	var equipmentFind []*__.EquipmentFind
	find, err := equipment.EquipmentFind()
	if err != nil {
		return nil, err
	}
	for _, b := range find {
		equipmentFind = append(equipmentFind, &__.EquipmentFind{
			Id:             int32(b.Id),
			BrandId:        b.BrandId,
			EquipmentName:  b.EquipmentName,
			EquipmentModel: b.EquipmentModel,
			Manufacturer:   b.Manufacturer,
		})
	}
	return equipmentFind, nil
}

// 设备详情
func EquipmentDetails(in *__.EquipmentDetailsReq) (*models.Equipment, error) {
	var equipment models.Equipment
	equipment.Id = uint64(in.Id)

	err := equipment.EquipmentDetails()
	if err != nil {
		return nil, err
	}

	return &equipment, nil
}
